package com.zhuoshi.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhuoshi.bean.WdContract;
import com.zhuoshi.bean.WdHtCustomer;
import com.zhuoshi.bean.WdHtRoom;
import com.zhuoshi.bean.WdHtRoomPay;
import com.zhuoshi.bean.WdOrder;
import com.zhuoshi.bean.WdPayType;
import com.zhuoshi.service.WdContractService;
import com.zhuoshi.service.WdHtCustomerService;
import com.zhuoshi.service.WdOrderService;
import com.zhuoshi.service.WdPayTypeService;
import com.zhuoshi.service.WdRoomPayService;
import com.zhuoshi.service.WdRoomService;

@Controller
@RequestMapping("/order")
public class WdOrderController {
	@Autowired
	private WdOrderService orderService;
	@Autowired
	private WdContractService contractService;
	@Autowired
	private WdRoomPayService roomPayService;
	@Autowired
	private WdRoomService roomService;
	@Autowired
	private WdHtCustomerService customerService;
	@Autowired
	private WdPayTypeService payTypeService;
	/**
	 * 查询所有
	 * @param model
	 * @return
	 */
	@RequestMapping("/list.do")
	public String orderList(Model model){
		List<WdOrder> orderList = orderService.selectOrderList();
		//System.out.println("list是否为空"+orderList.isEmpty());
		model.addAttribute("orderList", orderList);
		return "order_list";
	}
	/**
	 * 模糊查询
	 * @param order
	 * @param model
	 * @return
	 */
	@RequestMapping("/mohu.do")
	public String mohu(WdOrder order,Model model){
		List<WdOrder> orderList = orderService.mohu(order);
		System.out.println("list是否为空"+orderList.isEmpty());
		model.addAttribute("orderList", orderList);
		return "order_list";
	}
	/**
	 * 删除订单（把删除状态变为1）
	 */
	@RequestMapping("/delete.do")
	public String delete(String orderId){
		WdOrder model = null;
		String[] strs = orderId.split(",");
		for(String str : strs){
			model = new WdOrder();
			model.setOrderId(str);
			model.setDeleteState("1");
			orderService.updateByPrimaryKeySelective(model);
		}
		return "redirect:list.do";
	}
	/**
	 * 作废订单（把订单类型变为“作废”，删除状态变为2）
	 */
	@RequestMapping("/zuofei.do")
	public String zuofei(String orderId){
		WdOrder model = null;
		String[] strs = orderId.split(",");
		for(String str : strs){
			model = new WdOrder();
			model.setOrderId(str);
			model.setOrderType("作废");
			model.setDeleteState("2");
			orderService.updateByPrimaryKeySelective(model);
		}
		return "redirect:list.do";
	}
	/**
	 * 查询作废订单
	 */
	@RequestMapping("/zuofeiList.do")
	public String zuofeiList(Model model){
		List<WdOrder> list = orderService.selectzuofei();
		model.addAttribute("orderList", list);
		return "order_zuifei";
	}
	/**
	 * 恢复作废订单
	 */
	@RequestMapping("/huifu.do")
	public String huifu(String orderId){
		WdOrder model = null;
		String[] strs = orderId.split(",");
		for(String str : strs){
			model = new WdOrder();
			model.setOrderId(str);
			model.setOrderType("认购");
			model.setDeleteState("0");
			orderService.updateByPrimaryKeySelective(model);
		}
		return "redirect:zuofeiList.do";
	}
	/**
	 * 认购转签约
	 */
	@RequestMapping("/qianyue.do")
	public String qianyue(){
		WdOrder bean = new WdOrder();
		bean.setOrderType("认购");
		 List<WdOrder> mohu = orderService.mohu(bean);
		 for(WdOrder order : mohu){
			 String orderId = order.getOrderId();
			 //签约后把数据存到wd_contract表中
			 WdContract contract = new WdContract();
			 UUID uuid = UUID.randomUUID();
			 String uid = uuid.toString();
			 contract.setContractId(uid);
			 contract.setCustomerId(order.getCustomerId());
			 contract.setOrderId(orderId);
			 contract.setSignTime(order.getCreateTime());
			 contract.setContractTotal(order.getTotal()+"");
			 contract.setCustomerName(order.getCustomerName());
			 contract.setRoomId(order.getRoomId());
			 contract.setUnitPrice(order.getUnitPrice());
			 contract.setName(order.getName());
			 contractService.insertSelective(contract);
			 //把数据存到wd_ht_room_pay表中
			 WdHtRoomPay roomPay = new WdHtRoomPay();
			 WdContract selectContractId = contractService.selectContractId(orderId);
			 UUID uuuid = UUID.randomUUID();
			 String string = uuuid.toString();
			 roomPay.setRoomPayId(string);
			 roomPay.setContractId(selectContractId.getContractId());
			 roomPay.setOrderId(orderId);
			 roomPay.setReceivableMoney(order.getTotal());
			 roomPay.setRoomId(order.getRoomId());
			 roomPay.setCustomerName(order.getCustomerName());
			 roomPay.setReceivedMoney(0.0f);
			 roomPay.setUncollectedMoney(order.getTotal());
			 roomPay.setDeleteState("0");
			 roomPayService.insertSelective(roomPay);
			 //认购转成签约
			 WdOrder orderBean = new WdOrder();
			 orderBean.setOrderId(orderId);
			 orderBean.setOrderType("签约");
			 orderService.updateByPrimaryKeySelective(orderBean);
		 }
		return "redirect:list.do";
	}
	@RequestMapping("/add.do")
	public String add(Model model){
		List<WdHtCustomer> selectAllCustomer = customerService.getlist(null);
		model.addAttribute("selectAllCustomer",selectAllCustomer);
		WdPayType wbean= new WdPayType();
		wbean.setDeleteState("1");
		List<WdPayType> paylist = payTypeService.selectAll(wbean);
		model.addAttribute("paylist", paylist);
		List<WdHtRoom> selectAll = roomService.selectAll(null);
		model.addAttribute("selectRooms", selectAll);
		return "order_add";
	}
	@RequestMapping("/addOne")
	public String addOne(WdOrder bean) throws ParseException {
		WdHtCustomer selectByPrimaryKey = customerService.selectByPrimaryKey(bean.getCustomerId());
		bean.setCustomerName(selectByPrimaryKey.getCustomerName());
		String updateUser = bean.getUpdateUser();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = sdf.parse(updateUser);
		bean.setCreateTime(date);
		bean.setDeleteState("0");
		UUID uuid = UUID.randomUUID();
		String string = uuid.toString();
		bean.setOrderId(string);
		orderService.insertSelective(bean);
		return "redirect:list.do";
	}
	@RequestMapping("getUnit.do")
	public void getUnit(String roomId, HttpServletResponse response)
			throws Exception {
		WdHtRoom bean = roomService.selectByPrimaryKey(roomId);
		System.out.println( bean.getUnitPrice());
		String json = "{\"a\":\"" + bean.getUnitPrice() + "\",\"b\":\""
				+ bean.getUnitPrice() * bean.getInnerArea()
				+ "\",\"c\":\"" + bean.getInnerArea() + "\"}";
		response.getWriter().print(json);
	}
	@RequestMapping("/toupdate.do")
	public String toupdate(String orderId, Model model) {
		WdOrder model2 = orderService.selectByPrimaryKey(orderId);
		model.addAttribute("model", model2);
		List<WdPayType> paylist = payTypeService.selectAll(null);
		model.addAttribute("paylist", paylist);
		List<WdHtRoom> selectAll = roomService.selectAll(null);
		model.addAttribute("selectRooms", selectAll);
		return "order_update";
	}
	@RequestMapping("/update.do")
	public String update(WdOrder bean) {
		orderService.updateByPrimaryKeySelective(bean);
		return "redirect:list.do";
	}
	
}
